// main.cpp
#include "parallel_for.hpp"
#include <vector>
#include <chrono>
#include <iostream>
#include <cmath>
#include <omp.h>

constexpr size_t DATA_SIZES[] = {
    static_cast<size_t>(1e5),
    static_cast<size_t>(1e6),
    static_cast<size_t>(1e7),
    static_cast<size_t>(1e8),
    static_cast<size_t>(1e9)
};

inline double complex_operation(double f) {
    // 阶段1：超越函数组合
    double term1 = std::exp(std::sin(f));  // 嵌套超越函数
    
    // 阶段2：高次幂与条件分支
    double term2 = (f > 0.5) ? 
        std::cos(std::pow(f, 5)) :        // 分支路径A
        std::tan(std::sqrt(std::abs(f)));  // 分支路径B
    
    // 阶段3：迭代计算
    double term3 = 0;
    for(int i=0; i<4; ++i){  // 模拟牛顿迭代
        term3 = 0.5 * (term3 + f/term3);
    }
    
    return term1 * term2 + term3;
}

void test_thread_pool(size_t n) {
    std::vector<double> data(n, 1.5);
    auto start = std::chrono::high_resolution_clock::now();
    
    parallel_for(0, n, [&](size_t i) {
        data[i] = complex_operation(data[i]);
    });
    
    auto end = std::chrono::high_resolution_clock::now();
    std::cout << "ThreadPool: " 
              << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() 
              << " ms\n";
}

void test_openmp(size_t n) {
    std::vector<double> data(n, 1.5);
    auto start = std::chrono::high_resolution_clock::now();
    
    #pragma omp parallel for
    for(size_t i = 0; i < n; ++i) {
        data[i] = complex_operation(data[i]);
    }
    
    auto end = std::chrono::high_resolution_clock::now();
    std::cout << "OpenMP: " 
              << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() 
              << " ms\n";
}

int main() {
    for(auto size : DATA_SIZES) {
        std::cout << "\nTesting data size: " << size << std::endl;
        std::cout << "-------------------\n";
        test_thread_pool(size);
        test_openmp(size);
    }
    return 0;
}